package middleware.p2p.log;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;

/**
 * 	Classe rappresentante il log associato ad un evento all'interno del sistema
 */
@XmlRootElement(name="log_event")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(propOrder={"componentID","vclock","timestamp","description"})
@XmlSeeAlso(XmlHashtableType.class)
public class MiddlewareLogEvent implements XmlSerializable {
	/**
	 * 	Campo necessario alla serializzazione
	 */
	private static final long serialVersionUID = -7148717246253153643L;
	private String componentID;
	private VectorClock vclock;
	private long timestamp;
	private String description;
	/**
	 * @param componentID
	 * @param vclock
	 * @param timestamp
	 * @param description
	 */
	public MiddlewareLogEvent(String componentID, VectorClock vclock,
			long timestamp, String description) {
		super();
		this.componentID = componentID;
		this.vclock = vclock;
		this.timestamp = timestamp;
		this.description = description;
	}	
	/**
	 * @param vclock
	 * @param timestamp
	 * @param description
	 */
	public MiddlewareLogEvent(VectorClock vclock, String description) {
		this("",vclock,-1,description);		
	}
	public MiddlewareLogEvent() {
		this(null,new VectorClock(),-1,null);
	}
	/**
	 * @return the componentID
	 */
	@XmlElement
	public String getComponentID() {
		return componentID;
	}
	/**
	 * @return the vclock
	 */
	@XmlElementRef
	public VectorClock getVclock() {
		return vclock;
	}
	/**
	 * @return the timestamp
	 */
	@XmlElement
	public long getTimestamp() {
		return timestamp;
	}
	/**
	 * @return the description
	 */
	@XmlElement
	public String getDescription() {
		return description;
	}
	/**
	 * @param componentID the componentID to set
	 */
	public void setComponentID(String componentID) {
		this.componentID = componentID;
	}
	/**
	 * @param timestamp the timestamp to set
	 */
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	/**
	 * @param vclock the vclock to set
	 */
	public void setVclock(VectorClock vclock) {
		this.vclock = vclock;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return  "MWLE - "+	this.description;
	}
	@Override
	public String toXmlString() {
		try {
			JAXBContext context= JAXBContext.newInstance(this.getClass());
			Marshaller marshaller=context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			StringWriter sw=new StringWriter();			
			marshaller.marshal(this, sw);
			return sw.toString();
		} catch (JAXBException jaxbe) {
			jaxbe.printStackTrace();
			return "";
		}
	}
	
}
